package com.augurit.gzcgwsp.sendNotice.dao;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.augurit.common.base.dao.BaseDao;
import com.augurit.gzcgwsp.sendNotice.entity.YwWebserviceForWb;

/**
 * 网办（受理、办结），webservice方式交换给方欣
 * @author adm
 *
 */
@Repository
public class YwWebserviceForWbDao  extends BaseDao<YwWebserviceForWb, Long>{

	/**
	 * 保存webservice接口的信息
	 * @param ywWebserviceForWb
	 */
	public void save(YwWebserviceForWb ywWebserviceForWb){
		getSession().saveOrUpdate(ywWebserviceForWb);
	}
	
	/**
	 * 获得还没有发送的记录  网办接口
	 * @return
	 */
	public List<YwWebserviceForWb> getNoticeYwWebserviceForWb(){
		String hql = "from YwWebserviceForWb wb where wb.isSended is null" +
				" order by wb.produceDate desc"; 
		Query query = this.getSession().createQuery(hql);
		List<YwWebserviceForWb> list = query.list();
		return list;
	}
	
	
	
	/**
	 * 当webservice调用成功，则把改记录的isSended字段的值改成1
	 * @param id
	 */
	public void changState(long id){
		String hql = "update YwWebserviceForWb wb set wb.isSended='1' where wb.id=:id";
		Query query = this.getSession().createQuery(hql);
		query.setLong("id", id);
		query.executeUpdate();
	}
	
	/**
	 * 把改记录的isSended字段的值改成空，则定时器可以查到
	 * @param acceptNo系统受理号
	 * @param approvalNo方欣那边的受理号
	 */
	public void changStateToNullByAcceptNo(String acceptNo,String approvalNo){
		String hql = "update YwWebserviceForWb wb set wb.paraFour='"+approvalNo+"',wb.isSended=null where wb.acceptNo=:acceptNo and wb.paraThree='feedBackApprovalInfo'";
		Query query = this.getSession().createQuery(hql);
		query.setString("acceptNo", acceptNo);
		query.executeUpdate();
	}
	/**
	 * 根据acceptNo获取结束公示的一条记录
	 * @param acceptNo
	 * @return
	 */
	public YwWebserviceForWb getStopYwWebserviceForWbByAcceptNo(String acceptNo){
		String hql = "from YwWebserviceForWb wb where wb.paraThree='stopPublicApprovalInfo' and wb.acceptNo='"+acceptNo+"' and wb.isSended is null";
		Query query = this.getSession().createQuery(hql);
		List list = query.list();
		if(list != null && list.size() > 0){
			return (YwWebserviceForWb)list.get(0);
		}
		return null;
	}
}
